local skill = fk.CreateSkill{
  name = "tea__yingfa"
}
---@type ActiveSkillSpec
local yingfa = {
  anim_type = "offensive",
  mute = true,
  prompt = "#yingfa_prompt",
  target_num = 2,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected)
    return to_select ~= player and #selected < 2
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local ask1 = effect.tos[1]
    local ask2 = effect.tos[2]
    local targets = table.filter(room.alive_players, function(p)
      return p ~= ask1 and p ~= ask2
    end)
    room:notifySkillInvoked(player, self.name, "special")
    player:broadcastSkillInvoke(skill.name, 1)
    local to = room:askToChoosePlayers(player, {
      targets = targets,
      min_num = 1,
      max_num = 1,
      prompt = "#yingfa_target_choose-ask",
      skill_name = self.name,
      cancelable = false,
    })
    if #to == 0 then return end
    local seats = {ask1, ask2}
    room:sortByAction(seats)
    local answer = {}
    for i = 1, 2 do
      local ask = seats[i]
      local use = room:askToUseCard(ask, {
        skill_name = self.name,
        pattern = "slash",
        name = "slash",
        prompt = "#yingfa_use-ask:"..to[1].id,
        extra_data = {
          must_targets = {to[1].id},
          bypass_distances = true,
        },
      })
      if use then
        table.insert(answer, use)
      end
    end
    if #answer == 2 then
      room:notifySkillInvoked(player, self.name, "special")
      player:broadcastSkillInvoke(skill.name, 2)
      answer[1].tos = {player}
      answer[2].tos = {player}
    end
    if #answer > 0 then
      for _, using in ipairs(answer) do
        ---@type UseCardDataSpec
        using = using
        if not using.tos[1].dead and not using.from.dead
         and using.from:canUseTo(using.card, using.tos[1], {
          bypass_distances = true,
          bypass_times = true
         }) then
          room:useCard(using)
        end
      end
    end
    if #answer == 0 then
      player:broadcastSkillInvoke(skill.name, 3)
      local list = {}
      for _, p in ipairs({ask1, ask2}) do
        if not p:isNude() then
          table.insertIfNeed(list, p)
        end
      end
      if #list > 0 then
        local tos = room:askToChoosePlayers(player, {
          targets = list,
          skill_name = self.name,
          min_num = 1,
          max_num = 1,
          prompt = "#yingfa_obtain_choose-ask",
          cancelable = false,
        })
        if tos then
          local card = room:askToChooseCard(player, {
            target = tos[1],
            flag = "he",
            skill_name = self.name,
            prompt = "#yingfa_obtain-ask"
          })
          if card then
            room:obtainCard(player, card, false, fk.ReasonPrey, player.id)
          end
        end
      end
    end
  end,
}
skill:addEffect("active", yingfa)
skill:addTest(function (room, me)
  local comp2 = room.players[2]
  local comp3 = room.players[3]
  local comp4 = room.players[4]
  FkTest.runInRoom(function ()
    room:handleAddLoseSkills(me, skill.name)
  end)
  FkTest.setNextReplies(me, {json.encode{
    card = { skill = skill.name },
    targets = { comp2.id, comp3.id }
  }, json.encode{
    card = { skill = "choose_players_skill" },
    targets = { comp4.id }
  }, json.encode{
    card = { skill = "choose_players_skill" },
    targets = { comp2.id }
  }, json.encode{
    card = { skill = "choose_cards_skill", subcards = { table.random(comp2:getCardIds("h")) } },
    targets = {}
  } })
  FkTest.setNextReplies(comp2, {"__cancel", "__cancel", "__cancel", "__cancel", })
  FkTest.setNextReplies(comp3, {"__cancel", "__cancel", "__cancel", "__cancel", })

  FkTest.runInRoom(function ()
    GameEvent.Turn:create(TurnData:new(me, "game_rule", { Player.Play })):exec()
  end)
end)
return skill